package com.youlu.campus.web.platform;


import com.youlu.campus.service.platform.AppUrlLinkService;
import com.youlu.campus.service.platform.AppUrlSchemeService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
public class AppUrlSchemeController {

    @Autowired
   private AppUrlSchemeService appUrlSchemeService;

    @Autowired
    private AppUrlLinkService appUrlLinkService;

    @GetMapping("/public/url-scheme")
    public QResult getAppUrlScheme(String appId) {
        return QResult.success(appUrlSchemeService.getUrlScheme(appId));
    }


    @GetMapping("/public/url-link/{appId}")
    public void getAppUrlLink(@PathVariable String appId, HttpServletResponse response) {
        log.info("获取小程序链接 appId:{} URL Link {}",appId);
        String urlLink = appUrlLinkService.remoteUrlLink(appId, 2);
        log.info("获取小程序链接 appId:{} URL Link {}",appId,urlLink);
        try {
            response.sendRedirect(urlLink);
        } catch (IOException e) {
            log.error(":>>> 获取小程序连接IO错误:{}", e);
        }catch (Exception e) {
            log.error(":>>> 获取小程序连接其他错误:{}", e);
        }
    }
    @GetMapping("/public/app/token")
    public QResult getAppToken(){
        return QResult.success(appUrlSchemeService.getAppToken());
    }





}
